using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace DAL
{
    public class tbDatHangDAL
    {
        private SqlConnection cn;
        public tbDatHangDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public tbDatHangDAL(tbDatHangDAL dathang)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
        public void Insert(tbDatHang dathang)
        {
            SqlCommand cm = new SqlCommand("tbDatHang_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@deals", dathang.Deals);
            cm.Parameters.AddWithValue("@madh", dathang.MaDH);
            cm.Parameters.AddWithValue("@nguoimua", dathang.NguoiMua);
            cm.Parameters.AddWithValue("@ngaydat", dathang.NgayDat);
            cm.Parameters.AddWithValue("@diachinhan", dathang.DiaChiNhan);
            cm.Parameters.AddWithValue("@hinhthucthanhtoan", dathang.HinhThucThanhToan);
            cm.Parameters.AddWithValue("@dagiao", dathang.DaGiao);
            cm.Parameters.AddWithValue("@nhanviengiaohang", dathang.NhanVienGiaoHang);
            cm.ExecuteNonQuery();
        }

        public void Update(tbDatHang datHang)
        {
            SqlCommand cm = new SqlCommand("tbDatHang_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@stt", datHang.STT);
            cm.Parameters.AddWithValue("@deals", datHang.Deals);
            cm.Parameters.AddWithValue("@madh", datHang.MaDH);
            cm.Parameters.AddWithValue("@nguoimua", datHang.NguoiMua);
            cm.Parameters.AddWithValue("@diachinhan", datHang.DiaChiNhan);
            cm.Parameters.AddWithValue("@hinhthucthanhtoan", datHang.HinhThucThanhToan);
            cm.Parameters.AddWithValue("@dagiao", datHang.DaGiao);
            cm.Parameters.AddWithValue("@nhanviengiaohang",datHang.NhanVienGiaoHang);
            cm.ExecuteNonQuery();
        }

        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("tbDatHang_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@stt", iD);
            cm.ExecuteNonQuery();
        }

        public tbDatHangCollection Get(string where, int pageIndex, int pageSize,
           string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tbDatHang_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbDatHangCollection dhCollection = new tbDatHangCollection();

            while (dr.Read())
            {
                tbDatHang dh = new tbDatHang();
                dh.STT = int.Parse(dr.GetValue(0).ToString());
                dh.Deals = dr.GetValue(1).ToString();
                dh.MaDH = int.Parse(dr.GetValue(2).ToString());
                dh.NguoiMua = int.Parse(dr.GetValue(3).ToString());
                dh.NgayDat = DateTime.Parse(dr.GetValue(4).ToString());
                dh.DiaChiNhan = dr.GetValue(5).ToString();
                dh.HinhThucThanhToan = int.Parse(dr.GetValue(6).ToString());
                dh.DaGiao = Boolean.Parse(dr.GetValue(7).ToString());
                dh.NhanVienGiaoHang = int.Parse(dr.GetValue(8).ToString());
                dhCollection.Add(dh);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return dhCollection;
        }
    }
}
